//
//  Prototype.m
//  Charles
//
//  Created by Leo on 2019/6/6.
//  Copyright © 2019 charles. All rights reserved.
//

#import "Prototype.h"

@implementation Manager

- (NSMutableDictionary *)showCase {
    if (!_showCase) {
        _showCase = [[NSMutableDictionary alloc]initWithCapacity:0];
    }
    return _showCase;
}

- (void)registerName:(NSString *)name proto:(id<Product>)proto {
    if (name && proto) {
        [self.showCase setObject:proto forKey:name];
    }
}

- (id<Product>)create:(NSString *)protoName {
    id<Product>p = [self.showCase objectForKey:protoName];
    return [p createClone];
}

@end

@implementation MessageBox{
    // 保存的是像装饰方框那样的环绕着字符串的字符.
    char _decochar;
}
- (instancetype)initWithDecochar:(char)decochar{
    if (self = [super init]) {
        _decochar = decochar;
    }
    return self;
}

- (void)use:(NSString *)string {
    NSInteger length = string.length;
    for (unsigned int i = 0; i< length + 4; i++) {
        NSLog(@"%c",_decochar);
    }
    NSLog(@"\n");
    NSLog(@"%c %@ %c",_decochar,string,_decochar);
    for (unsigned int i = 0; i< length + 4; i++) {
        NSLog(@"%c",_decochar);
    }
    NSLog(@"\n");
}

- (id<Product>)createClone {
    id<Product>p = nil;
    @try {
        p = [self copy];
    } @catch (NSException *exception) {
        NSLog(@"%@",exception.description);
    }
    return p;
}

- (id)copyWithZone:(NSZone *)zone {
    MessageBox *messageBox = [[[self class] alloc]initWithDecochar:_decochar];
    return messageBox;
}

@end

@implementation UnderlinePen{
    // 保存的是像装饰方框那样的环绕着字符串的字符.
    char _ulchar;
}
- (instancetype)initWithUlchar:(char)ulchar{
    if (self = [super init]) {
        _ulchar = ulchar;
    }
    return self;
}

- (void)use:(NSString *)string {
    NSInteger length = string.length;
    NSLog(@"\" %@ \"",string);
    NSLog(@" ");
    for (unsigned int i = 0; i< length + 4; i++) {
        NSLog(@"%c",_ulchar);
    }
    NSLog(@"\n");
}

- (id<Product>)createClone {
    id<Product>p = nil;
    @try {
        p = [self copy];
    } @catch (NSException *exception) {
        NSLog(@"%@",exception.description);
    }
    return p;
}

- (id)copyWithZone:(NSZone *)zone {
    UnderlinePen *underlinePen = [[[self class] alloc]initWithUlchar:_ulchar];
    return underlinePen;
}


@end
